package cn.itcast.nems.product.scheduler;

import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.common.tenant.TenantInfoHolder;
import cn.itcast.nems.product.service.ProductService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 刷新产品状态任务
 *
 * @author yong.li
 * create: 2023-09-01 18:30:14
 **/
@Component
public class FlushProductStateJob {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final ProductService productService;

    public FlushProductStateJob(ProductService productService) {
        this.productService = productService;
    }

    @XxlJob("flushProductInvalidState")
    public ReturnT<String> flushProductInvalidState(String param) {
        long s = System.currentTimeMillis();
        logger.warn("Flush product invalid state job start with params {}...", param);
        AccountHolder.setSchedulerAccountInfo();
        TenantInfoHolder.setTenantInfo(new TenantInfo(TenantInfo.DEFAULT_TENANT_ID, TenantInfo.DEFAULT_SUBTENANT_ID));
        final int count = this.productService.flushInvalidState();
        String msg = String.format("更新了%d个商品状态为下架", count);
        long end = System.currentTimeMillis();
        logger.warn("{} 消耗 {}'ms", msg, (end - s));
        return new ReturnT<>(msg);
    }
}
